$(function(){
    require(['vue','utils'],function(Vue,utils){
        window.Vue=Vue;
        new Vue({
            el:'#appmain',
            data:{
                name:'',
                description:'',
                permission:'PUBLIC',
                projects:[],
                openProjects:[],
                loading:{project:null},
                userId:_userId_
            },
            mounted:function(){
                $('button[uk-close]>svg').remove();
                this.loadProjects(window.status);
            },
            methods:{
                importFile:function(pluginId,e){
                    if(e.target.files.length === 0){
                        toastr.error('请选择文件');
                        return;
                    }
                    var projectId = $('#project_id').val();
                    console.log("dddddddddddd"+projectId)
                    var file = e.target.files[0];
                    var fd = new FormData();
                    fd.append('file', file);
                    fd.append('pluginId', pluginId);
                    fd.append('projectId',projectId);
                    if (pluginId == 'cn.xiaoyaoji.import.mjson'){
                        utils.fileloader('/project/import', fd,
                            function (rs) {
                                location.reload();
                            });
                    }else {
                        utils.fileloader('/project/import/xml', fd,
                            function (rs) {
                                location.reload();
                            });
                    }
                },
                newProject:function(){
                    utils.post('/project',{name:this.name,description:this.description,permission:this.permission},function(rs){
                        location.href=x.ctx+'/doc/'+rs.data.docId+'/edit';
                    });
                },
                rename:function(id,name){
                    UIkit.modal.prompt('请输入新的名称',name).then(function(rs){
                        if(rs && rs != name){
                            utils.post('/project/'+id,{name:rs},function(){location.reload();});
                        }
                    });
                },
                archiveProject:function(id){
                    var self = this;
                    UIkit.modal.confirm('是否确认操作?').then(function() {
                        utils.post('/project/'+id+'/archive',{},function () {
                            self.loadProjects();
                        });
                    });
                },
                deleteProject:function (id) {   //
                    var self= this;
                    UIkit.modal.confirm('是否确认删除?').then(function() {
                        utils.delete('/project/'+id,function () {
                            self.loadProjects();
                        });
                    }, function () {
                    });
                },
                deleteActual:function (id) {  //彻底删除
                    UIkit.modal.confirm('是否确认删除,一旦删除无法恢复?').then(function() {
                        utils.delete('/project/'+id+"/actual",function () {
                            location.href='?_t='+Date.now();
                        });
                    }, function () {
                    });
                },
                restore:function(id){    //还原
                    utils.post('/project/'+id,{status:'VALID'},function(){
                        location.href=x.ctx+'/dashboard'
                    });
                },
                loadProjects:function(status){
                    var self = this;
                    self.loading.project=true;
                    utils.get('/project/list',{status:status },function(rs){
                        console.log(_userId_);
                        self.loading.project=false;
                        self.projects = rs.data.projects;
                        self.openProjects = rs.data.openProjects;
                    });
                },
                showHiddenProject: function(e){
                    //显示隐藏条目
                    var cbList = $('div.cb.public-item');
                    var hiddenList = new Array();
                    // console.log(hiddenList);
                    $.each(cbList, function (i,v) {
                        // alert(i + v);
                        var currDisplay = $(v).css("display");
                        if(currDisplay == "none"){
                            $(v).css("display", "block");
                            var hrefPartArr = $(v).find('div.prj-col a').attr('href').split('/');
                            var currProj = hrefPartArr[hrefPartArr.length - 1];
                            hiddenList.push(currProj);
                        }
                    });

                    if(hiddenList== null || hiddenList.length < 1){
                        alert("当前不存在隐藏项目!");
                        return;
                    }
                    console.log(hiddenList);
                    utils.post("/project/displayHidden",{hiddenList:hiddenList});

                },
                hiddenProject: function(e){
                    // var currE = e.target;
                    $(e.currentTarget).parent().parent().css('display','none');
                    // e.currentTarget.parentElement.parentElement.setAttribute("hidden","true");
                    var currProjHref = e.currentTarget.parentElement.parentElement.firstChild.firstChild.getAttribute('href');
                    var currProjArr = currProjHref.split('/');
                    var currProj = currProjArr[currProjArr.length - 1];
                    // alert('currProj' + currProj);
                    utils.post('/project/userProjHidden',{currProj:currProj});
                }
            },
            //added by yb
            updated:function(e){
                // $(".private-list").dragsort({dragSelector:"div.cb",dragSelectorExclude:"div.cb.home-p-title", dragEnd:this.$options.methods.storeOrder1});
                $(".private-list").dragsort({dragSelector:"div.cb",dragSelectorExclude:"div.cb.home-p-title", dragEnd:function (){var currElem = $(this);storeOrder(currElem);}});
            }
        });
    });
});

function storeOrder(currElem) {
    var hrefPartArr = $(currElem).find('div.prj-col a').attr('href').split('/');
    var currProj = hrefPartArr[hrefPartArr.length - 1];
    var currLoginUser = _userId_;
    var prevHref = $(currElem).prev().find('div.prj-col a').attr('href');
    var preProj
    if(prevHref == null){
        preProj = '00';
    }else{
        var preHrefPartArr = prevHref.split('/');
        preProj = preHrefPartArr== undefined ? '00' : preHrefPartArr[preHrefPartArr.length - 1];
    }

    //项目是在公开列表还是私有列表判断
    var permission = "PUBLIC";
    if($(currElem).hasClass("public-item")){
        permission = "PUBLIC";
    }
    if($(currElem).hasClass("private-item")){
        permission = "PRIVATE";
    }
    var utils = require('utils');
    utils.post('/project/sortUpdate',{old:currProj, young:preProj, permission:permission});
}
